package cn.supermarket.system.service.impl;

import cn.supermarket.common.contant.UserConstants;
import cn.supermarket.common.exception.ServiceException;
import cn.supermarket.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.supermarket.common.entity.system.SysPost;
import cn.supermarket.system.service.SysPostService;
import cn.supermarket.system.mapper.SysPostMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 *
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost>
    implements SysPostService{
    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return this.baseMapper.selectPostListByUserId(userId);
    }

    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post)
    {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(post.getPostCode()),SysPost::getPostCode,post.getPostCode());
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(post.getStatus()),SysPost::getStatus,post.getStatus());
        lambdaQueryWrapper.like(!StringUtils.isEmpty(post.getPostName()),SysPost::getPostName,post.getPostName());
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(SysPost post)
    {
        Long postId = checkPostId(post);
        SysPost info = this.baseMapper.checkPostNameUnique(post.getPostName());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post)
    {
        Long postId = checkPostId(post);
        SysPost info = this.baseMapper.checkPostCodeUnique(post.getPostCode());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    private Long checkPostId(SysPost post){
        return StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deletePostByIds(Long[] postIds)
    {
        for (Long postId : postIds)
        {
            SysPost post = this.baseMapper.selectById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return this.baseMapper.deleteBatchIds(Arrays.asList(postIds));
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId)
    {
        return this.baseMapper.countUserPostById(postId);
    }
}




